clear*

use "${root}/data/processed/final_sample.dta", clear

keep if baseline_sample == 1

* Education indicators

foreach educ_vars in ratio_StuTea_ps /// 
			avgschool_size_ps ///
			avgclass_size_ps ///
			teachers_100K_ps ///
			schools_100K_ps ///
			childc_estab_100K ///
			presch_estab_100K ///
			test_scores_ps ///
			prog_rate_ps ///
			ideb_ps {
					

		*local outcome = "`ideb'`t'"

		*** (1) baseline
		qui rdrobust res_`educ_vars'_3rd margin_mayor_left if baseline_sample==1, vce(cluster mun_code) all

		local coeff1_`educ_vars' : di %9.2f (e(tau_bc))
		local se1_`educ_vars' : di %9.2f (e(se_tau_rb))
		local pv1_`educ_vars' : di %9.2f (e(pv_rb))
		local n1_`educ_vars' = e(N)
		local effn1_`educ_vars' = e(N_h_l) + e(N_h_r)

		*** (2) lame-ducks subsample
		qui rdrobust res_`educ_vars'_3rd margin_mayor_left if lame_duck == 1, vce(cluster mun_code) all

		local coeff2_`educ_vars' : di %9.2f (e(tau_bc))
		local se2_`educ_vars' : di %9.2f (e(se_tau_rb))
		local pv2_`educ_vars' : di %9.2f (e(pv_rb))
		local n2_`educ_vars' = e(N)
		local effn2_`educ_vars' = e(N_h_l) + e(N_h_r)

		*** (3) Tiebout-below-median subsample
		qui rdrobust res_`educ_vars'_3rd margin_mayor_left if tiebout_median_sample == 1, vce(cluster mun_code) all

		local coeff3_`educ_vars' : di %9.2f (e(tau_bc))
		local se3_`educ_vars' : di %9.2f (e(se_tau_rb))
		local pv3_`educ_vars' : di %9.2f (e(pv_rb))
		local n3_`educ_vars' = e(N)
		local effn3_`educ_vars' = e(N_h_l) + e(N_h_r)

		*** (4) Ideology distance above median

		qui rdrobust res_`educ_vars'_3rd margin_mayor_left if coal_dist_median_sample == 1, vce(cluster mun_code) all

		local coeff4_`educ_vars' : di %9.2f (e(tau_bc))
		local se4_`educ_vars' : di %9.2f (e(se_tau_rb))
		local pv4_`educ_vars' : di %9.2f (e(pv_rb))
		local n4_`educ_vars' = e(N)
		local effn4_`educ_vars' = e(N_h_l) + e(N_h_r)

		*** (5) Oil-windfalls subsample

		qui rdrobust res_`educ_vars'_3rd margin_mayor_left if oil_sample == 1 , vce(cluster mun_code) all

		local coeff5_`educ_vars' : di %9.2f (e(tau_bc))
		local se5_`educ_vars' : di %9.2f (e(se_tau_rb))
		local pv5_`educ_vars' : di %9.2f (e(pv_rb))
		local n5_`educ_vars' = e(N)
		local effn5_`educ_vars' = e(N_h_l) + e(N_h_r)
	*}
}

* Estimate Violence Indicators

foreach homicides in homicide_rate{

		*** (1) baseline
		qui rdrobust res_`homicides'_avg margin_mayor_left if baseline_sample==1, vce(cluster mun_code) all

		local coeff1_`homicides' : di %9.2f (e(tau_bc))
		local se1_`homicides' : di %9.2f (e(se_tau_rb))
		local pv1_`homicides' : di %9.2f (e(pv_rb))
		local n1_`homicides' = e(N)
		local effn1_`homicides' = e(N_h_l) + e(N_h_r)

		*** (2) lame-ducks subsample
		qui rdrobust res_`homicides'_avg margin_mayor_left if lame_duck == 1, vce(cluster mun_code) all

		local coeff2_`homicides' : di %9.2f (e(tau_bc))
		local se2_`homicides' : di %9.2f (e(se_tau_rb))
		local pv2_`homicides' : di %9.2f (e(pv_rb))
		local n2_`homicides' = e(N)
		local effn2_`homicides' = e(N_h_l) + e(N_h_r)

		*** (3) Tiebout-below-median subsample
		qui rdrobust res_`homicides'_avg margin_mayor_left if tiebout_median_sample == 1, vce(cluster mun_code) all

		local coeff3_`homicides' : di %9.2f (e(tau_bc))
		local se3_`homicides' : di %9.2f (e(se_tau_rb))
		local pv3_`homicides' : di %9.2f (e(pv_rb))
		local n3_`homicides' = e(N)
		local effn3_`homicides' = e(N_h_l) + e(N_h_r)

		*** (4) Ideology distance above median

		qui rdrobust res_`homicides'_avg margin_mayor_left if coal_dist_median_sample == 1, vce(cluster mun_code) all

		local coeff4_`homicides' : di %9.2f (e(tau_bc))
		local se4_`homicides' : di %9.2f (e(se_tau_rb))
		local pv4_`homicides' : di %9.2f (e(pv_rb))
		local n4_`homicides' = e(N)
		local effn4_`homicides' = e(N_h_l) + e(N_h_r)

		*** (7) Oil-windfalls subsample

		qui rdrobust res_`homicides'_avg margin_mayor_left if oil_sample == 1 , vce(cluster mun_code) all

		local coeff5_`homicides' : di %9.2f (e(tau_bc))
		local se5_`homicides' : di %9.2f (e(se_tau_rb))
		local pv5_`homicides' : di %9.2f (e(pv_rb))
		local n5_`homicides' = e(N)
		local effn5_`homicides' = e(N_h_l) + e(N_h_r)

}


* Estimate Health Indicators

foreach health_vars in clinic_basic_100K ///
			clinic_total_100K ///
			docs_100K ///
			esf_100K ///
			infant_mort_rate {
	
		*** (1) baseline
		if "`health_vars'"=="esf_100K"{
			qui rdrobust res_`health_vars'_4th margin_mayor_left if baseline_sample==1, vce(cluster mun_code) all
		}
		else{
			qui rdrobust res_`health_vars'_avg margin_mayor_left if baseline_sample==1, vce(cluster mun_code) all

		}
		
		
		local coeff1_`health_vars' : di %9.2f (e(tau_bc))
		local se1_`health_vars' : di %9.2f (e(se_tau_rb))
		local pv1_`health_vars' : di %9.2f (e(pv_rb))
		local n1_`health_vars' = e(N)
		local effn1_`health_vars' = e(N_h_l) + e(N_h_r)

		*** (2) lame-ducks subsample
		if "`health_vars'"=="esf_100K"{
			qui rdrobust res_`health_vars'_4th margin_mayor_left if lame_duck == 1, vce(cluster mun_code) all
		}
		else{
			qui rdrobust res_`health_vars'_avg margin_mayor_left if lame_duck == 1, vce(cluster mun_code) all
		}
		
		local coeff2_`health_vars' : di %9.2f (e(tau_bc))
		local se2_`health_vars' : di %9.2f (e(se_tau_rb))
		local pv2_`health_vars' : di %9.2f (e(pv_rb))
		local n2_`health_vars' = e(N)
		local effn2_`health_vars' = e(N_h_l) + e(N_h_r)

		*** (3) Tiebout-below-median subsample
		if "`health_vars'"=="esf_100K"{
			qui rdrobust res_`health_vars'_4th margin_mayor_left if tiebout_median_sample == 1, vce(cluster mun_code) all
		}
		else{
			qui rdrobust res_`health_vars'_avg margin_mayor_left if tiebout_median_sample == 1, vce(cluster mun_code) all
		}
		
		local coeff3_`health_vars' : di %9.2f (e(tau_bc))
		local se3_`health_vars' : di %9.2f (e(se_tau_rb))
		local pv3_`health_vars' : di %9.2f (e(pv_rb))
		local n3_`health_vars' = e(N)
		local effn3_`health_vars' = e(N_h_l) + e(N_h_r)

		*** (4) Ideology distance above median
		if "`health_vars'"=="esf_100K"{
			qui rdrobust res_`health_vars'_4th margin_mayor_left if coal_dist_median_sample == 1, vce(cluster mun_code) all
		}
		else{
			qui rdrobust res_`health_vars'_avg margin_mayor_left if coal_dist_median_sample == 1, vce(cluster mun_code) all
		}
		
		local coeff4_`health_vars' : di %9.2f (e(tau_bc))
		local se4_`health_vars' : di %9.2f (e(se_tau_rb))
		local pv4_`health_vars' : di %9.2f (e(pv_rb))
		local n4_`health_vars' = e(N)
		local effn4_`health_vars' = e(N_h_l) + e(N_h_r)

		*** (5) Oil-windfalls subsample
		if "`health_vars'"=="esf_100K"{
			qui rdrobust res_`health_vars'_4th margin_mayor_left if oil_sample == 1 , vce(cluster mun_code) all
		}
		else{
			qui rdrobust res_`health_vars'_avg margin_mayor_left if oil_sample == 1 , vce(cluster mun_code) all
		}
		
		local coeff5_`health_vars' : di %9.2f (e(tau_bc))
		local se5_`health_vars' : di %9.2f (e(se_tau_rb))
		local pv5_`health_vars' : di %9.2f (e(pv_rb))
		local n5_`health_vars' = e(N)
		local effn5_`health_vars' = e(N_h_l) + e(N_h_r)
}

* write table
texdoc init "${root}/results/tables/table_aggregate_outcomes.tex", replace force


tex \begin{longtable}{lccccc}
tex \caption{RD estimates of the effect of a left-wing mayor on welfare-related outcomes} \\
tex \label{table_welfare_outcomes} \\
tex \toprule
tex 									&        Baseline 					& \multicolumn{4}{c}{Subsamples} 																													\\
tex \cmidrule(lr){2-2} \cmidrule(lr){3-6}

tex 									& 									& 	Lame Duck 						& Tiebout 	 			& Ideology  	& Oil windfall							\\
tex 									& 									& 	 						& $<$ median	 			& distance  	& 							\\
tex 									& 									& 	 						&	 			&  $>$ median  	& 				\\			
tex \toprule
tex \endhead \\

tex \multicolumn{6}{c}{Education outcomes} \\
tex \midrule

tex Student-teachers ratio  		& `coeff1_ratio_StuTea_ps' 	& `coeff2_ratio_StuTea_ps'	& `coeff3_ratio_StuTea_ps'	& `coeff4_ratio_StuTea_ps'	& `coeff5_ratio_StuTea_ps'		\\
tex  									& (`se1_ratio_StuTea_ps') 	& (`se2_ratio_StuTea_ps') 	& (`se3_ratio_StuTea_ps') 	& (`se4_ratio_StuTea_ps') 	& (`se5_ratio_StuTea_ps') 		\\
tex Average classroom size 		& `coeff1_avgclass_size_ps' 	& `coeff2_avgclass_size_ps'	& `coeff3_avgclass_size_ps'	& `coeff4_avgclass_size_ps'	& `coeff5_avgclass_size_ps'		\\
tex  									& (`se1_avgclass_size_ps') 	& (`se2_avgclass_size_ps') 	& (`se3_avgclass_size_ps') 	& (`se4_avgclass_size_ps') 	& (`se5_avgclass_size_ps') 		\\
tex Teachers, per 100K res.	& `coeff1_teachers_100K_ps' 	& `coeff2_teachers_100K_ps'	& `coeff3_teachers_100K_ps'	& `coeff4_teachers_100K_ps'	& `coeff5_teachers_100K_ps'		\\
tex  									& (`se1_teachers_100K_ps') 	& (`se2_teachers_100K_ps') 	& (`se3_teachers_100K_ps') 	& (`se4_teachers_100K_ps') 	& (`se5_teachers_100K_ps') 		\\
tex Schools, per 100K res.	& `coeff1_schools_100K_ps' 	& `coeff2_schools_100K_ps'	& `coeff3_schools_100K_ps'	& `coeff4_schools_100K_ps'	& `coeff5_schools_100K_ps'		\\
tex  									& (`se1_schools_100K_ps') 	& (`se2_schools_100K_ps') 	& (`se3_schools_100K_ps') 	& (`se4_schools_100K_ps') 	& (`se5_schools_100K_ps') 		\\
tex Child Care, per 100K res.	& `coeff1_childc_estab_100K' 	& `coeff2_childc_estab_100K'	& `coeff3_childc_estab_100K'	& `coeff4_childc_estab_100K'	& `coeff5_childc_estab_100K'		\\
tex  									& (`se1_childc_estab_100K') 	& (`se2_childc_estab_100K') 	& (`se3_childc_estab_100K') 	& (`se4_childc_estab_100K') 	& (`se5_childc_estab_100K') 		\\
tex Pre Schools, per 100K res.	& `coeff1_presch_estab_100K' 	& `coeff2_presch_estab_100K'	& `coeff3_presch_estab_100K'	& `coeff4_presch_estab_100K'	& `coeff5_presch_estab_100K'		\\
tex  									& (`se1_presch_estab_100K') 	& (`se2_presch_estab_100K') 	& (`se3_presch_estab_100K') 	& (`se4_presch_estab_100K') 	& (`se5_presch_estab_100K') 		\\
tex \midrule
tex Observations (all) 					&  `n1_teachers_100K_ps'    	& `n2_teachers_100K_ps'  		& `n3_teachers_100K_ps'  		& `n4_teachers_100K_ps'  		& `n5_teachers_100K_ps'  			\\
tex Observations (effective)			&  `effn1_teachers_100K_ps' 	& `effn2_teachers_100K_ps'		& `effn3_teachers_100K_ps' 	& `effn4_teachers_100K_ps'		& `effn5_teachers_100K_ps'   		\\
tex \midrule

tex IDEB progression rate 		& `coeff1_prog_rate_ps' 	& `coeff2_prog_rate_ps'	& `coeff3_prog_rate_ps'	& `coeff4_prog_rate_ps'	& `coeff5_prog_rate_ps'		\\
tex  									& (`se1_prog_rate_ps') 	& (`se2_prog_rate_ps') 	& (`se3_prog_rate_ps') 	& (`se4_prog_rate_ps') 	& (`se5_prog_rate_ps') 		\\
tex \midrule
tex Observations (all) 					&  `n1_prog_rate_ps'    	& `n2_prog_rate_ps'  		& `n3_prog_rate_ps'  		& `n4_prog_rate_ps'  		& `n5_prog_rate_ps'  			\\
tex Observations (effective)			&  `effn1_prog_rate_ps' 	& `effn2_prog_rate_ps'		& `effn3_prog_rate_ps' 	& `effn4_prog_rate_ps'		& `effn5_prog_rate_ps'   		\\
tex \midrule

tex IDEB test scores  		& `coeff1_test_scores_ps' 	& `coeff2_test_scores_ps'	& `coeff3_test_scores_ps'	& `coeff4_test_scores_ps'	& `coeff5_test_scores_ps'		\\
tex  									& (`se1_test_scores_ps') 	& (`se2_test_scores_ps') 	& (`se3_test_scores_ps') 	& (`se4_test_scores_ps') 	& (`se5_test_scores_ps') 		\\
tex IDEB overall index	 		& `coeff1_ideb_ps' 	& `coeff2_ideb_ps'	& `coeff3_ideb_ps'	& `coeff4_ideb_ps'	& `coeff5_ideb_ps'		\\
tex  									& (`se1_ideb_ps') 	& (`se2_ideb_ps') 	& (`se3_ideb_ps') 	& (`se4_ideb_ps') 	& (`se5_ideb_ps') 		\\
tex \midrule
tex Observations (all) 					&  `n1_ideb_ps'    	& `n2_ideb_ps'  		& `n3_ideb_ps'  		& `n4_ideb_ps'  		& `n5_ideb_ps'  			\\
tex Observations (effective)			&  `effn1_ideb_ps' 	& `effn2_ideb_ps'		& `effn3_ideb_ps' 	& `effn4_ideb_ps'		& `effn5_ideb_ps'   		\\
tex \midrule
tex \pagebreak
tex \multicolumn{6}{c}{Health outcomes} \\
tex \midrule

tex Clinics (basic), per 100K res.   & `coeff1_clinic_basic_100K' 	& `coeff2_clinic_basic_100K'	& `coeff3_clinic_basic_100K'	& `coeff4_clinic_basic_100K'	& `coeff5_clinic_basic_100K'		\\
tex  									& (`se1_clinic_basic_100K') 	& (`se2_clinic_basic_100K') 	& (`se3_clinic_basic_100K') 	& (`se4_clinic_basic_100K') 	& (`se5_clinic_basic_100K') 		\\
tex Clinics (total), per 100K res.  	& `coeff1_clinic_total_100K' 	& `coeff2_clinic_total_100K'	& `coeff3_clinic_total_100K'	& `coeff4_clinic_total_100K'	& `coeff5_clinic_total_100K'		\\
tex  									& (`se1_clinic_total_100K') 	& (`se2_clinic_total_100K') 	& (`se3_clinic_total_100K') 	& (`se4_clinic_total_100K') 	& (`se5_clinic_total_100K') 		\\
tex \midrule
tex Observations (all) 					&  `n1_clinic_basic_100K'    	& `n2_clinic_basic_100K'  		& `n3_clinic_basic_100K'  		& `n4_clinic_basic_100K'  		& `n5_clinic_basic_100K'  			\\
tex Observations (effective)			&  `effn1_clinic_basic_100K' 	& `effn2_clinic_basic_100K'		& `effn3_clinic_basic_100K' 	& `effn4_clinic_basic_100K'		& `effn5_clinic_basic_100K'   		\\
tex \midrule

tex ESF team, per 100K res. 		& `coeff1_esf_100K' 	& `coeff2_esf_100K'	& `coeff3_esf_100K'	& `coeff4_esf_100K'	& `coeff5_esf_100K'		\\
tex  									& (`se1_esf_100K') 	& (`se2_esf_100K') 	& (`se3_esf_100K') 	& (`se4_esf_100K') 	& (`se5_esf_100K') 		\\
tex \midrule
tex Observations (all) 					&  `n1_esf_100K'    	& `n2_esf_100K'  		& `n3_esf_100K'  		& `n4_esf_100K'  		& `n5_esf_100K'  			\\
tex Observations (effective)			&  `effn1_esf_100K' 	& `effn2_esf_100K'		& `effn3_esf_100K' 	& `effn4_esf_100K'		& `effn5_esf_100K'   		\\
tex \midrule

tex Doctors, per 100K res. 		& `coeff1_docs_100K' 	& `coeff2_docs_100K'	& `coeff3_docs_100K'	& `coeff4_docs_100K'	& `coeff5_docs_100K'		\\
tex  									& (`se1_docs_100K') 	& (`se2_docs_100K') 	& (`se3_docs_100K') 	& (`se4_docs_100K') 	& (`se5_docs_100K') 		\\
tex \midrule
tex Observations (all) 					&  `n1_docs_100K'    	& `n2_docs_100K'  		& `n3_docs_100K'  		& `n4_docs_100K'  		& `n5_docs_100K'  			\\
tex Observations (effective)			&  `effn1_docs_100K' 	& `effn2_docs_100K'		& `effn3_docs_100K' 	& `effn4_docs_100K'		& `effn5_docs_100K'   		\\
tex \midrule

tex Infant mortality 		& `coeff1_infant_mort_rate' 	& `coeff2_infant_mort_rate'	& `coeff3_infant_mort_rate'	& `coeff4_infant_mort_rate'	& `coeff5_infant_mort_rate'		\\
tex  									& (`se1_infant_mort_rate') 	& (`se2_infant_mort_rate') 	& (`se3_infant_mort_rate') 	& (`se4_infant_mort_rate') 	& (`se5_infant_mort_rate') 		\\


tex \midrule
tex Observations (all) 					&  `n1_infant_mort_rate'    	& `n2_infant_mort_rate'  		& `n3_infant_mort_rate'  		& `n4_infant_mort_rate'  		& `n5_infant_mort_rate'  			\\
tex Observations (effective)			&  `effn1_infant_mort_rate' 	& `effn2_infant_mort_rate'		& `effn3_infant_mort_rate' 	& `effn4_infant_mort_rate'		& `effn5_infant_mort_rate'   		\\

tex \midrule
tex \multicolumn{6}{c}{Law enforcement outcomes} \\
tex \midrule

tex Homicide rate						& `coeff1_homicide_rate'		& `coeff2_homicide_rate'		& `coeff3_homicide_rate'			& `coeff4_homicide_rate'		& `coeff5_homicide_rate'		\\
tex  									& (`se1_homicide_rate') 		& (`se2_homicide_rate') 		& (`se3_homicide_rate') 			& (`se4_homicide_rate') 		& (`se5_homicide_rate') 		\\

tex \midrule
tex Observations (all) 					&  `n1_homicide_rate'    		& `n2_homicide_rate' 			& `n3_homicide_rate'  				& `n4_homicide_rate'  			& `n5_homicide_rate'  			\\
tex Observations (effective)			&  `effn1_homicide_rate'		& `effn2_homicide_rate'			& `effn3_homicide_rate'   			& `effn4_homicide_rate'			& `effn5_homicide_rate'   	\\

tex \bottomrule
tex \end{longtable}


texdoc close
